//
//  MJ_OwnerOperationController.m
//  UEAssistant
//
//  Created by mac on 2017/12/28.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "MJ_OwnerOperationController.h"
#import "MJ_OperationViewModel.h"
#import "MJ_TaskTableViewCell.h"
#import "UIColor+Extend.h"
#import "MJ_ReportViewController.h"
#import "MJ_SearchBar.h"
#import "MJ_SearchViewController.h"
#import "MJ_ScreeningButton.h"
#import <Masonry.h>
#import "MJ_ScreenViewController.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#import "MJ_ProjectDetailController.h"
@interface MJ_OwnerOperationController ()<UITableViewDelegate, UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

/**
 项目监管/生产报表切换
 */
@property (strong, nonatomic)UISegmentedControl *segment;

@property (strong, nonatomic)UITableView *tableView;

@property (strong, nonatomic)MJ_OperationViewModel *viewModel;

@property (strong, nonatomic)MJ_ReportViewController *reportVC;

/**
 记录上次点击按钮，防止重复点击，重复操作
 */
@property (assign, nonatomic)NSInteger lastSelectedIndex;
/**
 搜索框
 */
@property (strong, nonatomic)MJ_SearchBar *searchView;

/**
 筛选按钮
 */
@property (strong, nonatomic)UIButton *screeningButton;
@end

@implementation MJ_OwnerOperationController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-44) style:UITableViewStylePlain];
        [_tableView registerClass:[MJ_TaskTableViewCell class] forCellReuseIdentifier:@"MJ_TaskTableViewCell"];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = [UIColor colorWithHexString:@"#e0e0e0"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.emptyDataSetSource = self;
    }
    return _tableView;
}

- (MJ_OperationViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[MJ_OperationViewModel alloc] init];
    }
    return _viewModel;
}

- (MJ_ReportViewController *)reportVC {
    if (!_reportVC) {
        _reportVC = [[MJ_ReportViewController alloc] init];
    }
    return _reportVC;
}

- (MJ_SearchBar *)searchView {
    if (!_searchView) {
        _searchView = [[MJ_SearchBar alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 44)];
        _searchView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        
    }
    return _searchView;
}

- (UIButton *)screeningButton {
    if (!_screeningButton) {
        _screeningButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_screeningButton setTitle:@"筛选" forState:UIControlStateNormal];
        [_screeningButton setImage:[UIImage imageNamed:@"筛选"] forState:UIControlStateNormal];
        [_screeningButton setBackgroundImage:[UIImage imageNamed:@"BG"] forState:UIControlStateNormal];
        _screeningButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _screeningButton.layer.cornerRadius = 20.0;
        _screeningButton.layer.masksToBounds = YES;
        _screeningButton.titleEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 0);
        _screeningButton.imageEdgeInsets = UIEdgeInsetsMake(0, 55, 0, 0);
        [_screeningButton setTitleColor:mainColor forState:UIControlStateNormal];
        [_screeningButton addTarget:self action:@selector(screenButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _screeningButton;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _lastSelectedIndex = 0;
    [self loadDataWithParameter:@{}];
    [self setupUI];
    

}
//项目监管列表
- (void)loadDataWithParameter:(NSDictionary *)parameter {
    [LSProgressHUD showLoadingToView:self.navigationController.view];
    __weak typeof(self) wSelf = self;
    [self.viewModel loadProjectManagerWithParameter:@{@"pagenum":@"1",
                                                      @"pagesize":@"500",
                                                      @"projectNameKeyword":@"",
                                                      @"sourcelangkey":@"",
                                                      @"targetlangkey":@"",
                                                      @"vmId":@"0"} success:^(BOOL success) {
                                                          __strong typeof(wSelf) sSelf = wSelf;
                                                          if (success) {
                                                              [sSelf.tableView reloadData];
                                                          }
                                                          [LSProgressHUD hideForView:sSelf.navigationController.view];
                                                          [sSelf.tableView.mj_footer endRefreshing];
                                                          [sSelf.tableView.mj_header endRefreshing];
                                                      }];
}

- (void)setupUI {
    //----------------------segment设置----------------------------------------//
    NSArray *array = [NSArray arrayWithObjects:@"项目监控",@"生产报表", nil];
    _segment = [[UISegmentedControl alloc]initWithItems:array];
    _segment.frame = CGRectMake(10, 100, 152*Radio, 28);
    _segment.center = CGPointMake(self.view.center.x, 20);
    NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:11],
                               NSForegroundColorAttributeName:[UIColor whiteColor]};
    NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:11],
                             NSForegroundColorAttributeName:RGB(133, 136, 146, 1)};
    [_segment setTitleTextAttributes:normal forState:UIControlStateNormal];
    [_segment setTitleTextAttributes:selected forState:UIControlStateSelected];
    _segment.tintColor = RGB(133, 136, 146, 1);
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _segment;
    //----------------------tableView设置----------------------------------------//
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.searchView;

    //----------------------其他跳转----------------------------------------//
    //点击搜索跳转
    __weak typeof(self) wSelf = self;
    self.searchView.ClickSearchBarBlock = ^(UIButton *button) {
        __strong typeof(wSelf) sSelf = wSelf;
        MJ_SearchViewController *vc = [[MJ_SearchViewController alloc] init];
        UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:vc];
        vc.projectArray = sSelf.viewModel.projectList;
        [sSelf presentViewController:nv animated:YES completion:nil];
    };
    
    [self.view addSubview:self.screeningButton];
    [self.screeningButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-74);
        make.width.mas_equalTo(100);
    }];
}
#pragma mark --UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.projectList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MJ_TaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MJ_TaskTableViewCell" forIndexPath:indexPath];
    cell.cellLayout = self.viewModel.projectList[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MJ_TaskCellLayout *layout = self.viewModel.projectList[indexPath.row];
    return layout.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MJ_ProjectDetailController *detailVc = [[MJ_ProjectDetailController alloc] init];
    MJ_TaskCellLayout *layout = self.viewModel.projectList[indexPath.row];
    detailVc.model = layout.projectModel;
    [self.navigationController pushViewController:detailVc animated:YES];
}

#pragma mark - privite method
- (void)segmentPressed:(UISegmentedControl *)sender {
    
    if (_lastSelectedIndex != _segment.selectedSegmentIndex) {
        
        if (_segment.selectedSegmentIndex == 1) {
            [self addChildViewController:self.reportVC];
            [self.view addSubview:self.reportVC.view];
            
        }else{
            [self.reportVC.view removeFromSuperview];
            [self.reportVC removeFromParentViewController];
        }
        _lastSelectedIndex = _segment.selectedSegmentIndex;
    }
}

- (void)screenButtonPressed:(UIButton *)sender {
    __weak typeof(self) wSelf = self;
    MJ_ScreenViewController *vc = [[MJ_ScreenViewController alloc] init];
    vc.SelectLanKeyBlock = ^(NSString *sourceKey, NSString *targetKey) {
        [LSProgressHUD showLoadingToView:self.navigationController.view];
        __strong typeof(wSelf) sSelf = wSelf;
        [sSelf.viewModel loadProjectWithSourceKey:sourceKey targetKey:targetKey success:^(BOOL success) {
            if (success) {
                [sSelf.tableView reloadData];
            }
            [LSProgressHUD hideForView:sSelf.navigationController.view];

        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark --空数据处理
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"任务"];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *text = @"空空如也~";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    
    return YES;
}

@end
